<template>
  <div>
    <j-modal
      title="档案"
      :width="500"
      :visible="isVisible"
      switchFullscreen
      @ok="handleOk"
      @cancel="handleCancel"
      cancelText="关闭">
      <ul style="list-style-type:none; margin:0; padding:0; margin-bottom: 10px;">
        <li v-for="(pdffile, i) in pdfFileList" v-if="pdffile.status == 0">
          <a-icon type="copy" style="margin-right:8px;"/>{{pdffile.name}}<a-icon type="delete" @click="delPDF(i)" style="margin-left:30px;"/>
        </li>
      </ul>
      <j-upload @change="uploadChange" :initDate="true" :showUploadList="false" :returnUrl="false" :trigger-change="true"></j-upload>
    </j-modal>
  </div>
</template>

<script>
  import JUpload from '@/components/jeecg/JUpload'
  import {getPdfList, savePDF} from '@/api/api'

  export default {
    name: "TraineePdfDocument",
    components: {
      JUpload,
    },
    data () {
      return {
        userId: '',
        pdfFileList: []
      }
    },
    props: {
      isVisible:{
        type:Boolean,
        required:false,
        default: false
      }
    },
    methods:{
      uploadChange(file) {
        for (var i = 0; i < file.length; i++) {
          var dIndex = file[i].fileName.lastIndexOf(".");
          var item = {
            'id': '',
            'name': file[i].fileName.substring(0, dIndex),
            'file': file[i].filePath,
            'status': 0
          }
          this.pdfFileList.push(item);
        }
      },
      delPDF(idx) {
        let pdfItem = this.pdfFileList[idx];
        this.pdfFileList.splice(idx, 1)
        pdfItem.status = 1
        this.pdfFileList.push(pdfItem);
      },
      savePDF() {

      },
      loadDate (userid) {
        this.userId = userid
        this.pdfFileList = []
        //查询学员档案
        var paramter = {
          'objectType': 1,
          'objectId': this.userId
        }
        getPdfList(paramter).then(res=>{
          if(res.success) {
            for(var i = 0; i < res.result.length; i++) {
              for (var i = 0; i < res.result.length; i++) {
                console.log(res.result[i])
                var item = {
                  'id': res.result[i].id,
                  'name': res.result[i].name,
                  'file': res.result[i].file,
                  'status': 0
                }
                this.pdfFileList.push(item);
              }
            }
          }else {
            that.$message.error("请求异常："+err)
          }
        })
      },
      changeUrl(file){
        console.log(file)
      },
      add () {
        this.isVisible=true
      },
      edit (record) {
        this.isVisible=true
        this.$nextTick(()=>{
          this.$refs.realForm.edit(record);
        })
      },
      close () {
        this.$emit('cancel');
      },
      handleOk () {
        var that = this
        if(this.pdfFileList == null || this.pdfFileList.length == 0) {
          that.$message.error("无保存数据")
          this.close();
        }else {
          // 保存
          var paramter = {
            'userId': that.userId,
            'dtoList': JSON.stringify(this.pdfFileList)
          }
          savePDF(paramter).then(res=>{
            console.log(res)
            if(res.success) {
              that.$message.success("修改成功")
              this.close();
            }else {
              that.$message.error("修改失败："+res.message)
            }
          })
        }
      },
      submitCallback(){
        this.isVisible = false;
      },
      handleCancel () {
        this.close()
      }
    },
    model: {
      prop: 'value',
      event: 'change'
    }
  }
</script>

<style scoped>

</style>